package cn.tq365.util.google;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

import com.google.api.adwords.lib.AdWordsUser;

/**
 * 谷歌同步工具类
 * 
 * @author lili
 * 
 */
public class SyncGoogleUtil {
	public static final Properties props = new Properties();
	public static final String apiPath = Thread.currentThread()
			.getContextClassLoader().getResource("google-api.properties")
			.getPath();
	static {
		// 该类在第一次被调用的时候执行
		try {
			// 读取配置文件
			props.load(new FileInputStream(apiPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static List<AdWordsUser> getAdWordsUsers() throws Exception {
		int i = 0;
		List<AdWordsUser> userList = new ArrayList<AdWordsUser>();
		for (Entry<Object, Object> entry : props.entrySet()) {
			String key = entry.getKey().toString();
			String value = entry.getValue().toString();
			if (key.contains("email")) {
				String addr = key.split("_")[0];
				AdWordsUser user = new AdWordsUser(value,
						props.getProperty(addr + "_password"),
						props.getProperty("useragent"),
						props.getProperty("developerToken"));
				userList.add(user);
				if (++i < 3) {
					continue;
				} else {
					break;
				}
			}
		}
		// System.out.println(JSONArray.fromObject(userList));
		return userList;
	}

	public static void main(String[] args) throws Exception {
		SyncGoogleUtil.getAdWordsUsers();
	}
}
